package text;

// Fruit.java - 水果基类
public abstract class Fruit {
    protected String name;
    protected double price; // 单价
    protected double discount; // 折扣，1.0表示原价

    public Fruit(String name, double price) {
        this.name = name;
        this.price = price;
        this.discount = 1.0;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    public double getDiscount() {
        return discount;
    }

    public void setDiscount(double discount) {
        if (discount < 0 || discount > 1) {
            throw new IllegalArgumentException("折扣必须在0-1之间");
        }
        this.discount = discount;
    }

    // 计算该水果的总价
    public double calculatePrice(int weight) {
        if (weight < 0) {
            throw new IllegalArgumentException("水果斤数必须大于等于0");
        }
        return weight * price * discount;
    }

    @Override
    public String toString() {
        return String.format("%s(%.1f元/斤)", name, price);
    }
}

// 具体水果类
class Apple extends Fruit {
    public Apple() {
        super("苹果", 8.0);
    }
}

class Strawberry extends Fruit {
    public Strawberry() {
        super("草莓", 13.0);
    }
}

class Mango extends Fruit {
    public Mango() {
        super("芒果", 20.0);
    }
}